package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.missilike.biz.IMilAccountService;
import com.missilike.biz.IMilFollowRelationshipService;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilFollowRelationship;
import com.missilike.repository.mapper.MilFollowRelationshipMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
public class MilFollowRelationshipServiceImpl extends ServiceImpl<MilFollowRelationshipMapper, MilFollowRelationship> implements IMilFollowRelationshipService {

    private  IMilAccountService accountService;


    @Override
    public List<MilAccountInfo> watchingUsers(String userId) {
        QueryWrapper<MilFollowRelationship> friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFollowRelationship::getAccountId, userId);
        List<MilFollowRelationship> followRelationshipList = baseMapper.selectList(friendsWrapper);
        if (followRelationshipList.size() == 0) {
            return new ArrayList<>();
        }
        Set<String> friendIds = followRelationshipList.stream().map(MilFollowRelationship::getFollowAccountId).collect(Collectors.toSet());
        List<MilAccountInfo> friends = accountService.getUserByIds(friendIds);
        return friends;
    }

    @Override
    public  Integer getBeFollowedCount(String userId) {
        QueryWrapper<MilFollowRelationship> friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFollowRelationship::getFollowAccountId, userId);
        List<MilFollowRelationship> followRelationshipList = baseMapper.selectList(friendsWrapper);
        if (followRelationshipList.size() == 0) {
            return 0;
        }
        List<String> friendIds = followRelationshipList.stream().map(MilFollowRelationship::getAccountId).collect(Collectors.toList());
        return friendIds.size();
    }

    @Override
    public List<MilAccountInfo> getFollowuser(String userId){
        QueryWrapper<MilFollowRelationship> friendsWrapper = new QueryWrapper();
        friendsWrapper.lambda().eq(MilFollowRelationship::getAccountId, userId);
        List<MilFollowRelationship> followRelationshipList = baseMapper.selectList(friendsWrapper);
        if (followRelationshipList.size() == 0) {
            return new ArrayList<>();
        }
        Set<String> friendIds = followRelationshipList.stream().map(MilFollowRelationship::getFollowAccountId).collect(Collectors.toSet());
        List<MilAccountInfo> friends = accountService.getUserByIds(friendIds);
        return friends;
    }

    @Override
    public CommonResult InsertRelationship(String accountId, String followAccountId) {
        QueryWrapper<MilFollowRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", accountId);
        wrapper.eq("follow_account_id", followAccountId);
        List<MilFollowRelationship> followRelationshipList = baseMapper.selectList(wrapper);
        if (followRelationshipList.size() > 0) {
            return ResultUtils.ok();
        }
        MilAccount followAccount = accountService.getById(followAccountId);
        MilFollowRelationship relationship = new MilFollowRelationship();
        relationship.setAccountId(accountId);
        relationship.setFollowAccountId(followAccountId);
        relationship.setFollowAccountHeadId(followAccount.getFaceUrlId());
        relationship.setFollowAccount(followAccount.getAccount());
        relationship.setFollowAccountNickName(followAccount.getNickName());
        relationship.setCreateTime(LocalDateTime.now());
        baseMapper.insert(relationship);
        return ResultUtils.ok();
    }

    @Override
    public  CommonResult deleteRelationship(String accountId, String followAccountId){
        QueryWrapper<MilFollowRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", accountId);
        wrapper.eq("follow_account_id", followAccountId);
        baseMapper.delete(wrapper);
         return  ResultUtils.ok();
    }


    @Override
    public boolean isAFollowB(String followId, String id) {
        QueryWrapper<MilFollowRelationship> followRelationshipQueryWrapper = new QueryWrapper<>();
        followRelationshipQueryWrapper.lambda().eq(MilFollowRelationship::getAccountId, id).eq(MilFollowRelationship::getFollowAccountId, followId);
        return baseMapper.selectList(followRelationshipQueryWrapper).size() > 0;
    }

    @Override
    public void setFollowRelationshipService(MilAccountServiceImpl milAccountService) {
        this.accountService = milAccountService;
    }

    public void addMutualConcern(String accountId,String friendAccountId){
        assembleMilFollowRelationship(accountId,friendAccountId);
        assembleMilFollowRelationship(friendAccountId,accountId);
    }

    public void deleteMutualConcern(String accountId,String friendAccountId){
        removeMilFollowRelationship(accountId,friendAccountId);
        removeMilFollowRelationship(friendAccountId,accountId);
    }

    private MilFollowRelationship getMilFollowRelationship(String accountId,String friendAccountId){
        return getOne(new QueryWrapper<MilFollowRelationship>().
                lambda().eq(MilFollowRelationship::getAccountId, accountId).
                eq(MilFollowRelationship::getFollowAccountId,friendAccountId));
    }

    private void assembleMilFollowRelationship(String accountId,String friendAccountId){
        MilFollowRelationship milFollowRelationship= getMilFollowRelationship(accountId,friendAccountId);
        if (milFollowRelationship==null){
            MilAccountInfo milAccountInfo= accountService.getAccount(accountId);
            milFollowRelationship=new MilFollowRelationship();
            milFollowRelationship.setAccountId(accountId);
            milFollowRelationship.setFollowAccountId(friendAccountId);
            milFollowRelationship.setFollowAccountNickName(milAccountInfo.getNickName());
            milFollowRelationship.setFollowAccount(milAccountInfo.getAccount());
            milFollowRelationship.setFollowAccountHeadId(milAccountInfo.getFaceUrlId());
            this.save(milFollowRelationship);
        }
    }

    private void removeMilFollowRelationship(String accountId,String friendAccountId){
        MilFollowRelationship milFollowRelationship= getMilFollowRelationship(accountId,friendAccountId);
        if (milFollowRelationship!=null){
            this.removeById(milFollowRelationship.getId());
        }
    }
}
